package com.zjmzxfzhl.modules.app.controller;

import java.util.Arrays;

import javax.validation.Valid;

import com.zjmzxfzhl.modules.app.entity.AppStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppAlarmSet;
import com.zjmzxfzhl.modules.app.service.AppAlarmSetService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/alarmSet")
public class AppAlarmSetController extends BaseController {
    @Autowired
    private AppAlarmSetService appAlarmSetService;

    /**
     * 自定义查询列表
     * 
     * @param appAlarmSet
     * @param current
     * @param size
     * @return
     */
//    @PreAuthorize("@elp.single('app:alarmSet:list')")
    @GetMapping(value = "/list")
    public Result list(AppAlarmSet appAlarmSet, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<AppAlarmSet> pageList = appAlarmSetService.list(new Page<AppAlarmSet>(current, size), appAlarmSet);
        return Result.ok(pageList);
    }

    @PreAuthorize("@elp.single('app:alarmSet:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppAlarmSet appAlarmSet = appAlarmSetService.getById(id);
        return Result.ok(appAlarmSet);
    }

    /**
     * @功能：新增
     * @param appAlarmSet
     * @return
     */
//    @PreAuthorize("@elp.single('app:alarmSet:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppAlarmSet appAlarmSet) {
        Boolean bool =  appAlarmSetService.saveAppAlarmSet(appAlarmSet);
        if (bool == false) {
            return Result.error("添加失败");
        }
        AppAlarmSet result = appAlarmSetService.queryById(appAlarmSet.getSetId());
        return Result.ok(result);
    }

    /**
     * @功能：修改
     * @param appAlarmSet
     * @return
     */
    @PreAuthorize("@elp.single('app:alarmSet:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppAlarmSet appAlarmSet) {
        Boolean result=appAlarmSetService.updateAppAlarmSet(appAlarmSet);
        if(result==false){
            return Result.error("修改失败！");
        }
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
//    @PreAuthorize("@elp.single('app:alarmSet:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            appAlarmSetService.removeByIds(Arrays.asList(idsArr));
        } else {
            appAlarmSetService.removeById(idsArr[0]);
        }
        return Result.ok();
    }
}
